﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-8-12
Description:CustomerList三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class CustomerListService
	{
		#region 增加一条数据
		public static int AddCustomerList(CustomerList customerList)
		{
			string sql =
			"INSERT INTO CustomerList(xingming,xingbie,zhengjiantype,zhengjianID) "+
			"VALUES (@xingming,@xingbie,@zhengjiantype,@zhengjianID)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@xingming",customerList.Xingming),
						
					new SqlParameter("@xingbie",customerList.Xingbie),
						
					new SqlParameter("@zhengjiantype",customerList.Zhengjiantype),
						
					new SqlParameter("@zhengjianID",customerList.ZhengjianID)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteCustomerListById(long id)	
		{
			string sql="DELETE "+"CustomerList"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifyCustomerListByid(CustomerList customerList)
		{
		string sql ="UPDATE "+"CustomerList"+" set "+
					"xingming=@xingming, "+
					"xingbie=@xingbie, "+
					"zhengjiantype=@zhengjiantype, "+
					"zhengjianID=@zhengjianID "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",customerList.Id),
					new SqlParameter("@xingming",customerList.Xingming),
					new SqlParameter("@xingbie",customerList.Xingbie),
					new SqlParameter("@zhengjiantype",customerList.Zhengjiantype),
					new SqlParameter("@zhengjianID",customerList.ZhengjianID)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<CustomerList> GetAllCustomerLists()
		{
			string sqlAll = "SELECT * FROM CustomerList";
			return GetCustomerListsBySql(sqlAll);
		}
		//以标识列获得对象
		public static CustomerList GetCustomerListByid(object id)
		{
			string sql = "SELECT * FROM "+"CustomerList"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					CustomerList customerList = new CustomerList();
					customerList.Id=Convert.ToInt32(reader["id"]);
					customerList.Xingming=Convert.ToString(reader["xingming"]);
					customerList.Xingbie=Convert.ToString(reader["xingbie"]);
					customerList.Zhengjiantype=Convert.ToString(reader["zhengjiantype"]);
					customerList.ZhengjianID=Convert.ToString(reader["zhengjianID"]);
					reader.Close();
					
					return customerList;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<CustomerList> GetCustomerListsBySql(string safeSql)
		{
			List<CustomerList> list = new List<CustomerList>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					CustomerList customerList = new CustomerList();
					customerList.Id=Convert.ToInt32(row["id"]);
					customerList.Xingming=Convert.ToString(row["xingming"]);
					customerList.Xingbie=Convert.ToString(row["xingbie"]);
					customerList.Zhengjiantype=Convert.ToString(row["zhengjiantype"]);
					customerList.ZhengjianID=Convert.ToString(row["zhengjianID"]);
					list.Add(customerList);
				}
				return list;
			}
             
		}
		private static IList<CustomerList> GetCustomerListsBySql( string sql, params SqlParameter[] values )
		{
			List<CustomerList> list = new List<CustomerList>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					CustomerList customerList = new CustomerList();
					customerList.Id=Convert.ToInt32(row["id"]);
					customerList.Xingming=Convert.ToString(row["xingming"]);
					customerList.Xingbie=Convert.ToString(row["xingbie"]);
					customerList.Zhengjiantype=Convert.ToString(row["zhengjiantype"]);
					customerList.ZhengjianID=Convert.ToString(row["zhengjianID"]);
					list.Add(customerList);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<CustomerList> ShowCustomerListForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from CustomerList where  id not in ( select top "
						   +number+" id from CustomerList order by id desc ) order by id desc";
			return GetCustomerListsBySql(strsql);
		}
		#endregion
	}
}

